package br.great.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import br.great.classes.TexteRange;
import br.great.beans.InvertedIndex;
import br.great.functionalCore.TagIndexCreator;

/**
 * PhotoMap
 * Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
 */
/**
 * @author Developpement of PhotoMap
 */
public class DAO_InvertedIndex {

    private PreparedStatement insertStatement;
    private static volatile DAO_InvertedIndex instaceDAO_InvertedIndex = new DAO_InvertedIndex();

    /** The private constructor of DAO_InvertedIndex for singleton
     */
    private DAO_InvertedIndex() {
        try {
            PgConnect conn = PgConnect.getInstance();
            insertStatement = conn.executeInsertWithPreparedStatement(getInsertStatement());
        } catch (Exception ex) {
            System.err.println("Problems to prepare statement");
        }
    }
   public String getInsertStatement(){
        String insertTag= "insert into Inverted_Index values(nextval('seqmax_InvertedIndex'),?,1,?,?);";
        return  insertTag; 
    }
    /** The method for get the instance of the class DAO_InvertedIndex
     * @return DAO_InvertedIndex instaceDAO_InvertedIndex
     */
    public static DAO_InvertedIndex getInstance() {
        //if(instaceDAO_InvertedIndex==null)
        synchronized (DAO_InvertedIndex.class) {
            if (instaceDAO_InvertedIndex == null) {
                instaceDAO_InvertedIndex = new DAO_InvertedIndex();
            }
        }
        return instaceDAO_InvertedIndex;
    }

    public int insertInvertedIndex(InvertedIndex invIndex, String stamp) {
        int i=0;
        PgConnect conn = PgConnect.getInstance();
        if (invIndex != null) {
            String iiValueEntier = TexteRange.tradeText(invIndex.getTAG_VALUE());
            if (!iiValueEntier.equalsIgnoreCase("null")) {
               // String[] iis = iiValueEntier.split(" ");

                //if (iis.length == 1) {
                    String iiValue = iiValueEntier + stamp;
                    // get invIndex for looking for the invIndex is existe or not
                    String getTag = "select * from Inverted_Index where Lower(tag_value)=Lower('" + iiValue + "') AND Lower(TAG_TYPE)=Lower('"+invIndex.getTAG_TYPE()+"') AND Lower(TAG_SUBTYPE)=Lower('"+invIndex.getTAG_SUB_TYPE()+"');";
                    // insert into the Tag a new value

                    String getIdTag = "select last_value as max from seqmax_invertedindex";

                    try {
                        ResultSet rs_getTag = conn.executeQuery(getTag);
                        // if the invIndex is in the table
                        if (rs_getTag != null) {
                            if (rs_getTag.next()) {
                                // after do the insert, get the value of the ID Tag
                                i = rs_getTag.getInt("INVERTED_INDEX_ID");
                                String updateCounter = "update Inverted_Index set tag_counter=tag_counter+1 where INVERTED_INDEX_ID=" + i + ";";
//System.out.println(updateCounter);
                                conn.executeInsert(updateCounter);

                            } else { // if th invIndex isn't in table, we insert the invIndex in it

                              
                                insertStatement.setString(1, iiValue);
                                insertStatement.setString(2, invIndex.getTAG_TYPE());
                                insertStatement.setString(3, invIndex.getTAG_SUB_TYPE());
                                insertStatement.execute();
                             //   String insertTag = "insert into Inverted_Index values(nextval('seqmax_InvertedIndex'),'" + iiValue + "',1,'" + invIndex.getTAG_TYPE() + "','" + invIndex.getTAG_SUB_TYPE() + "');";
                               // conn.executeInsert(insertTag);
                                  //Calculatig the max id of the table invIndex
                                try {
                                    ResultSet rs = conn.executeQuery(getIdTag);
                                    if (rs.next()) {
                                        i = rs.getInt("max");
                                    }else{
                                    i=0;
                                    }    
                                } catch (Exception e) {
                                    //First InvertedIndex
                                    e.printStackTrace();
                                    i = 0;
                                } 
                             //   System.err.println(insertTag);
                               // System.err.println(i);
                            }
                        }
                    } catch (SQLException ex) {
                        System.err.println("error of insert InvertedIndex");
                        ex.getStackTrace();
                    }
                       }
        }
        conn.Close();
        return i;
    }

//                } else {
//                    for (int j = 0; j < iis.length + 1; j++) {
//                        String iiValue = "";
//                        if (j == iis.length) {
//                            iiValue = iiValueEntier + stamp;
//                        } else {
//                            iiValue = iis[j] + stamp;
//                        }
//                        // get invIndex for looking for the invIndex is existe or not
//                        String getTag = "select * from Inverted_Index where Lower(tag_value)=Lower('" + iiValue + "');";
//                        // insert into the Tag a new value
//
//                        String getIdTag = "select last_value as max from seqmax_invertedindex";
//
//                        try {
//                            ResultSet rs_getTag = conn.executeQuery(getTag);
//                            // if the invIndex is in the table
//                            if (rs_getTag != null) {
//                                if (rs_getTag.next()) {
//                                    // after do the insert, get the value of the ID Tag
//                                    i = rs_getTag.getInt("INVERTED_INDEX_ID");
//                                    String updateCounter = "update Inverted_Index set tag_counter=tag_counter+1 where INVERTED_INDEX_ID=" + i + ";";
////System.out.println(updateCounter);
//                                    conn.executeInsert(updateCounter);
//
//                                } else { // if th invIndex isn't in table, we insert the invIndex in it
//
//                                    //Calculatig the max id of the table invIndex
//                                    try {
//                                        ResultSet rs = conn.executeQuery(getIdTag);
//                                        while (rs.next()) {
//                                            i = rs.getInt("max");
//                                        }
//                                    } catch (Exception e) {
//                                        //First InvertedIndex
//                                        e.printStackTrace();
//                                        i = 0;
//                                    }
//                                    i = i + 1;
//
//                                    String insertTag = "insert into Inverted_Index values(nextval('seqmax_InvertedIndex'),'" + iiValue + "',1,'" + invIndex.getTAG_TYPE() + "');";
//                                    conn.executeInsert(insertTag);
//                                }
//                            }
//                        } catch (SQLException ex) {
//                            System.out.println("error of insert InvertedIndex");
//                            ex.getStackTrace();
//                        }
//                    }
//                }
         
    public void insertFriendInvertedIndex(Vector<String> vsFriends, int idMultimedia) {
        DAO_InvertedIndexHasPhoto diihp = DAO_InvertedIndexHasPhoto.getInstance();
        TagIndexCreator tagIndex = new TagIndexCreator();
        InvertedIndex iv = new InvertedIndex();
        for (int j = 0; j < vsFriends.size(); j++) {
            iv.setTAG_VALUE(vsFriends.get(j));
            iv.setTAG_TYPE("FRIEND");
            iv.setTAG_SUB_TYPE("FRIEND");
            int iTag = this.insertInvertedIndex(iv, tagIndex.getPersonTag(""));
            diihp.insertInvertedIndexPhoto(iTag, idMultimedia, 1);
        }
    }

//    public static void main(String[] args) {
//        DAO_InvertedIndex dt = DAO_InvertedIndex.getInstance();
//        InvertedIndex tag = new InvertedIndex();
//        tag.setTAG_VALUE("par.what");
//        tag.setTAG_TYPE("par");
//        dt.insertInvertedIndex(tag, ".WHAT");
//    }
}
